package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;

/**
 * 资信数据
 * @author liubaoxin
 */
@Deprecated
public class NFCSParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  String id="";
	private String timestamstr="";
	private String job_id ="";
	private String id_no="";
	private StringBuilder sb = new StringBuilder(500);
	private  Text val = new Text();
	private  Text key= new Text();
	/**
	 * 资信数据
	 * @param userBehavior
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseNFCS(Context context,NfcsReport nfcsReport,String successful) throws IOException, InterruptedException{
    	Job job = nfcsReport.getJob();
    	NfcsData nfcsData = nfcsReport.getData();
    	CrawlResult result=job.getResult();
    	sb.delete(0,sb.length());
    	//数据时间
    	this.timestamstr=nfcsReport.getTimestamp()==null?"":DateUtil.formateDateStr(new Date(nfcsReport.getTimestamp()));
    	//身份证号
    	this.id_no =nfcsData.getCertno()==null?"":nfcsData.getCertno();
    	//job_id
    	this.job_id = job.getJobID()==null?"":job.getJobID();
    	this.id = MD5Util.getMd5(this.job_id+this.timestamstr+this.id_no);
 
    	sb.append(this.id).append(CRT001).append(job_id).append(CRT001).append(this.timestamstr).append(CRT001);
    	//响应http状态 "name", "certtype",  "reason", "createtype","data" 
    	sb.append(nfcsData.getName()==null?"":nfcsData.getName()).append(CRT001);
    	sb.append(nfcsData.getCerttype()==null?"":nfcsData.getCerttype()).append(CRT001);
    	sb.append(nfcsData.getCertno()==null?"":nfcsData.getCertno()).append(CRT001);
    	sb.append(nfcsData.getReason()==null?"":nfcsData.getReason()).append(CRT001);
    	sb.append(nfcsData.getCreatetype()==null?"":nfcsData.getCreatetype()).append(CRT001);
    	sb.append(nfcsData.getData()==null?"":nfcsData.getData()).append(CRT001);
    	//是否爬取完成
    	sb.append(job.isFinished()==null?"":job.isFinished()).append(CRT001);
    	//是否有请求 
    	sb.append(job.isRequested()==null?"":job.isRequested()).append(CRT001);
    	//是否爬取成功 
//    	sb.append(job.isSuccessful()!=null && job.isSuccessful()?"1":"0").append(CRT001);
    	sb.append(successful).append(CRT001);
    	//爬取开始时间
    	sb.append(job.getStartTime()==null?"":DateUtil.formateDateStr(job.getStartTime())).append(CRT001);
    	//爬取结束时间
    	sb.append(job.getEndTime()==null?"":job.getEndTime()).append(CRT001);
    	//数据爬取业务系统
    	sb.append(job.getSystemID()==null?"":job.getSystemID()).append(CRT001);
    	//手机号码
    	sb.append(job.getQuery()==null?"":job.getQuery()).append(CRT001);
    	//重试次数
    	sb.append(job.getTryTimes()==null?"":job.getTryTimes()).append(CRT001);
    	//爬取结果类型 SUCCESSFUL
    	sb.append(result==null?"":result.getType()).append(CRT001);
//    	sb.append("SUCCESSFUL").append(CRT001);
    	//爬取结果消息
    	sb.append(result==null?"":result.getMessage()).append(CRT001);
    	//消息code
    	sb.append(result==null?"":result.getCode());
    	key.set("nfcs_data");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
	}
	
	
}
